added SSCLI 1.0
[windows-sources.git] / sdk / samples / all in on code / Visual Studio 2008 / CppLoadLibrary / ReadMe.txt
blobeedc44bbb1744e23d9928526c1d5b5a5c98d1406
1 =============================================================================
2           CONSOLE APPLICATION : CppLoadLibrary Project Overview
3 =============================================================================
5 /////////////////////////////////////////////////////////////////////////////
6 Summary:
8 This is an example of dynamically loading a DLL using the APIs LoadLibrary, 
9 GetProcAddress and FreeLibrary. In contrast with implicit linking (static 
10 loading), dynamic loading does not require the LIB file, and the application 
11 loads the module just before we call a function in the DLL. The API functions 
12 LoadLibrary and GetProcAddress are used to load the DLL and then retrieve the
13 address of a function in the export table. Because we explicitly invoke these 
14 APIs, this kind of loading is also referred to as explicit linking. 
17 /////////////////////////////////////////////////////////////////////////////
18 Sample Relation:
20 CppLoadLibrary -> CppDynamicLinkLibrary
21 CppLoadLibrary dynamically loads CppDynamicLinkLibrary.dll and calls the 
22 functions exported by the DLL.
24 CppLoadLibrary - CSLoadLibrary
25 CSLoadLibrary in C# mimics the behavior of CppLoadLibrary to dynamically load
26 a native DLL, get the address of a function in the export table, and call it.
29 /////////////////////////////////////////////////////////////////////////////
30 Implementation:
32 1. Type-define the functions exported from the DLL. For example:
34         typedef int (* LPFNGETSTRINGLENGTH1)    (PWSTR);
36 2. Dynamically load the DLL by calling LoadLibrary.
38     hModule = LoadLibrary(pszModuleName);
40 3. Call GetProcAddress to get the address of the function in the export table 
41 of the DLL.
43     LPFNGETSTRINGLENGTH1 lpfnGetStringLength1 = (LPFNGETSTRINGLENGTH1) 
44         GetProcAddress(hModule, "GetStringLength1");
46 4. Call the function.
48     PWSTR pszString = L"HelloWorld";
49     int nLength;
50     nLength = lpfnGetStringLength1(pszString);
52 5. Unload the library by calling FreeLibrary.
54     FreeLibrary(hModule);
57 /////////////////////////////////////////////////////////////////////////////
58 References:
60 MSDN: Using Run-Time Dynamic Linking
61 http://msdn.microsoft.com/en-us/library/ms686944.aspx
64 /////////////////////////////////////////////////////////////////////////////